package org.csu.mypetstore.service;

import org.csu.mypetstore.domain.Category;
import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Product;
import org.csu.mypetstore.persistence.CategoryMapper;
import org.csu.mypetstore.persistence.ItemMapper;
import org.csu.mypetstore.persistence.PetMapper;
import org.csu.mypetstore.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class PetService {
    @Autowired
    PetMapper petMapper;
    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;

    public List<Item> getItemList(String categoryId){
        List<Product> productList = petMapper.getProductList(categoryId);
        List<Item> petItemList = new ArrayList<>();
        for(int i=0;i<productList.size();i++){
            List<Item> ItemList = itemMapper.getItemListByProduct(productList.get(i).getProductId());
            for(int j=0;j<ItemList.size();j++){
                    Item item = ItemList.get(j);
                    item.setQuantity(itemMapper.isOnsale(item.getItemId()));
                    Product product = productMapper.getProduct(item.getProductId());
                    product.setProductId(item.getProductId());
                    item.setProduct(product);
                    petItemList.add(item);
            }
        }
        return petItemList;
    }

    public void unShelve(String itemId){
        petMapper.unShelve(itemId);
    }

    public void upDate(String itemId, String productId, BigDecimal listPrice, int quantity, String name){
        Item item = new Item();
        item.setQuantity(quantity);
        item.setItemId(itemId);
        item.setListPrice(listPrice);
        Product product = new Product();
        product.setProductId(productId);
        product.setName(name);
        petMapper.upDate1(item);
        petMapper.upDate2(product);
        petMapper.upDate3(item);
    }

    public Item getItem(String itemId){
        return petMapper.getItem(itemId);
    }
    public void upDateItem(String itemId,String attribute1){
        Item item = new Item();
        item.setItemId(itemId);
        item.setAttribute1(attribute1);
        petMapper.upDateItem(item);
    }

    public void addNew(String categoryId,String name,String description,String productId,String itemId,BigDecimal listPrice,BigDecimal unitCost,String attribute1,int quantity){
        Category category = categoryMapper.getCategory(categoryId);
        if(category==null){
            category = new Category();
            category.setCategoryId(categoryId);
            category.setName(categoryId);
            category.setDescription(categoryId);
            petMapper.addNewCategory(category);
        }
        Product product = productMapper.getProduct(productId);
        if(product==null){
            product=new Product();
            product.setProductId(productId);
            product.setName(name);
            product.setDescription(description);
            product.setCategoryId(categoryId);
            petMapper.addNewProduct(product);
        }
        Item item = itemMapper.getItem(itemId);
        if(item==null){
            item = new Item();
            item.setAttribute1(attribute1);
            item.setItemId(itemId);
            item.setListPrice(listPrice);
            item.setUnitCost(unitCost);
            item.setProductId(productId);
            item.setSupplierId(1);
            item.setStatus("P");
            item.setQuantity(quantity);
            petMapper.addNewItem(item);
            petMapper.addNewInventory(item);
        }
    }
}
